/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * PmTeamService
 * 项目组成员
 * <p>
 * 修改纪录
 * 2020-05-12 版本：1.0 wushenghong 创建。
 *
 * @version 版本：1.0
 * @author 作者：wushenghong
 * 创建日期：2020-05-12
 */
package com.topscomm.pm.service.impl;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.pm.pojo.PmTaskEntity;
import com.topscomm.pm.pojo.PmTeamEntity;
import com.topscomm.pm.service.IPmTeamService;
import com.topscomm.pm.service.impl.auto.PmTeamServiceAuto;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service(value = "pmTeamService")
@Scope(value = "singleton")
public class PmTeamService extends PmTeamServiceAuto implements IPmTeamService {
    /**
     * @author: wushenghong
     * @date: 2020/6/12
     * @description: 删除控制,支持批量删除
     */
    @Override
    public void deleteBefore(String whereSql) throws ServiceException {
        super.deleteBefore(whereSql);
        List<PmTeamEntity> pmTeamEntityList = this.queryByWhere(whereSql);
        if (pmTeamEntityList.size() > 0) {
            for (PmTeamEntity pmTeamEntity : pmTeamEntityList) {
                String userCode = pmTeamEntity.getUsercode();
                Long projectId = pmTeamEntity.getProjectid();
                if (isTaskMember(userCode, projectId)) {
                    throw new ServiceException("所选项目组成员已存在任务记录，不允许删除！");
                }
            }
        }
    }

    /**
     * @author: wushenghong
     * @date: 2020/6/12
     * @description: 判断是否是当前项目的任务成员
     */
    public boolean isTaskMember(String userCode, Long projectId) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectid = {0}", projectId);
        PmTaskService pmTaskService = (PmTaskService) SpringUtil.getBean("pmTaskService");
        List<PmTaskEntity> pmTaskEntityList = pmTaskService.queryByWhere(sql.toString());
        if (pmTaskEntityList.size() > 0) {
            for (PmTaskEntity pmTaskEntity : pmTaskEntityList) {
                if (pmTaskEntity.getLeadercode().equals(userCode) || Arrays.asList(pmTaskEntity.getParticipantcodes().split(",")).contains(userCode)) {
                    return true;
                }
            }
        }
        return false;
    }
}
